package org.ehe.system.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.ehe.system.domain.bo.HrEmployeeFamilyBo;
import org.ehe.system.domain.vo.HrEmployeeFamilyVo;
import org.ehe.system.service.IHrEmployeeFamilyService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 员工家庭成员管理
 *
 * @author zhangnn
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/employee/family")
public class HrEmployeeFamilyController extends BaseController {

    private final IHrEmployeeFamilyService familyService;

    /**
     * 查询员工家庭成员列表
     */
    @SaCheckPermission("system:employee:list")
    @GetMapping("/list")
    public TableDataInfo<HrEmployeeFamilyVo> list(HrEmployeeFamilyBo bo, PageQuery pageQuery) {
        return familyService.queryPageList(bo, pageQuery);
    }

    /**
     * 根据工号查询员工家庭成员列表
     *
     * @param employeeNo 工号
     */
    @SaCheckPermission("system:employee:query")
    @GetMapping("/list/{employeeNo}")
    public R<List<HrEmployeeFamilyVo>> listByEmployeeNo(@PathVariable String employeeNo) {
        List<HrEmployeeFamilyVo> list = familyService.queryListByEmployeeNo(employeeNo);
        // 返回时提示数据已加密
        return R.ok(list);
    }

    /**
     * 获取员工家庭成员详细信息
     *
     * @param familyId 家庭成员主键
     */
    @SaCheckPermission("system:employee:query")
    @GetMapping("/{familyId}")
    public R<HrEmployeeFamilyVo> getInfo(@PathVariable Long familyId) {
        return R.ok(familyService.queryById(familyId));
    }

    /**
     * 新增员工家庭成员
     */
    @SaCheckPermission("system:employee:add")
    @Log(title = "员工家庭成员", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HrEmployeeFamilyBo bo) {
        boolean result = familyService.insertByBo(bo);
        if (result) {
            return R.ok("数据已加密保存");
        }
        return R.fail();
    }

    /**
     * 修改员工家庭成员
     */
    @SaCheckPermission("system:employee:edit")
    @Log(title = "员工家庭成员", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HrEmployeeFamilyBo bo) {
        boolean result = familyService.updateByBo(bo);
        if (result) {
            return R.ok("数据已加密更新");
        }
        return R.fail();
    }

    /**
     * 删除员工家庭成员
     *
     * @param familyIds 家庭成员主键串
     */
    @SaCheckPermission("system:employee:remove")
    @Log(title = "员工家庭成员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{familyIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] familyIds) {
        return toAjax(familyService.deleteByIds(Arrays.asList(familyIds)));
    }

}
